package com.pickgliss.ui.controls
{
	import com.pickgliss.events.FrameEvent;
	import com.pickgliss.geom.InnerRectangle;
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.utils.ClassUtils;
	import com.pickgliss.utils.DisplayUtils;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class MinimizeFrame extends Frame
	{
		public static const P_minimizeButton:String = "minimizeButton";
		public static const P_minimizeRect:String = "minimizeInnerRect";
		
		protected var _minimizeButton:BaseButton;
		protected var _minimizeInnerRect:InnerRectangle;
		protected var _minimizeRectString:String;
		protected var _minimizeStyle:String;
		public function MinimizeFrame()
		{
			super();
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			if(_minimizeButton)addChild(_minimizeButton);
		}
		
		/**
		 * 
		 * @param value 设置图标的InnerRectangle
		 * 具体请看InnerRectangle
		 * 
		 */		
		public function set minimizeRectString(value:String):void
		{
			if(_minimizeRectString == value)return;
			_minimizeRectString = value;
			_minimizeInnerRect = ClassUtils.CreatInstance(ClassUtils.INNERRECTANGLE,ComponentFactory.parasArgs(_minimizeRectString));
			onPropertiesChanged(P_closeInnerRect);
		}
		
		/**
		 * 
		 * 最小化按钮
		 * 
		 */		
		public function get minimizeButton():BaseButton
		{
			return _minimizeButton;
		}
		public function set minimizeButton(button:BaseButton):void
		{
			if(_minimizeButton == button) return;
			if(_minimizeButton)
			{
				_minimizeButton.removeEventListener(MouseEvent.CLICK,__onMinimizeClick);
				ObjectUtils.disposeObject(_minimizeButton);
			}
			_minimizeButton = button;
			onPropertiesChanged(P_minimizeButton);
		}
		
		/**
		 * 
		 * @param stylename 最小化按钮的样式
		 * 通过ComponentFactory 创建
		 */		
		public function set minimizeStyle(stylename:String):void
		{
			if(_minimizeStyle == stylename) return;
			_minimizeStyle =  stylename;
			minimizeButton = ComponentFactory.Instance.creat(_minimizeStyle);
		}
		
		protected function updateMinimizePos():void
		{
			if(_minimizeButton && _minimizeInnerRect)
			{
				DisplayUtils.layoutDisplayWithInnerRect(_minimizeButton,_minimizeInnerRect,_width,_height);
			}
		}
		
		override protected function onProppertiesUpdate():void
		{
			super.onProppertiesUpdate();
			if(_changedPropeties[P_minimizeButton])
			{
				_minimizeButton.addEventListener(MouseEvent.CLICK,__onMinimizeClick);
			}
			
			if(_changedPropeties[P_minimizeButton] || _changedPropeties[P_minimizeRect])
			{
				updateMinimizePos();
			}
		}
		
		protected function __onMinimizeClick(event:MouseEvent):void
		{
			onResponse(FrameEvent.MINIMIZE_CLICK);
		}
		
		override public function dispose():void
		{
			super.dispose();
			if(_minimizeButton)
			{
				_minimizeButton.removeEventListener(MouseEvent.CLICK,__onMinimizeClick);
				ObjectUtils.disposeObject(_minimizeButton);
			}
			_minimizeButton = null;
		}
	}
}